package aaa.schedule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;

/**
 *
 * @author Seb
 */
public class Lesson implements Comparable<Lesson>, Parcelable {

	private long lessonId;
	private String title;
	private String time;
	private String schedule;
	private int type;
	private String rooms;
	private String groups;
	private String teachers;
	private String created;
	private Date date;

	public Lesson(long lessonId, String title, String time, String schedule, int type, String room, String groups, String teacher, String created) {
		this.lessonId = lessonId;
		this.title = title;
		this.time = time;
		this.schedule = schedule;
		this.type = type;
		this.rooms = room;
		this.groups = groups;
		this.teachers = teacher;
		this.created = created;
		this.date = null ;
	}

	public Lesson(Parcel in) {
		this(in.readLong(), in.readString(), in.readString(), in.readString(), 
				in.readInt(), in.readString(), in.readString(), in.readString(), in.readString());
	}

	public Lesson() {
	}

	public void setGroups(String groups) {
		this.groups = groups;
	}

	public void setLessonId(long lessonId) {
		this.lessonId = lessonId;
	}

	public void setRoom(String room) {
		this.rooms = room;
	}

	public void setSchedule(String schedule) {
		this.schedule = schedule;
	}

	public void setTeachers(String teacher) {
		this.teachers = teacher;
	}

	public void setTime(String time) {
		this.time = time;
		this.date =null ;
	}

	public void setCreated(String created) {
		this.created = created;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getGroups() {
		return groups;
	}

	public long getLessonId() {
		return lessonId;
	}

	public String getRoom() {
		return rooms;
	}

	public String getSchedule() {
		return schedule;
	}

	public String getTeacher() {
		return teachers;
	}

	public String getTime() {
		return time;
	}

	public String getCreated() {
		return created;
	}

	public String getTitle() {
		return title;
	}

	public int getType() {
		return type;
	}

	@Override
	public String toString(){
		return String.format("ID : %s\nCours : '%s'\nNom de l'EDT associé : '%S'\nType :" +
				" %s\nSalle(s) : '%s'\nGoupes : '%s'\nEnseignant(s) : '%s'\nHeure : '%s'", 
				lessonId, title, schedule, type, rooms, groups, teachers, time);
	}

	public int compareTo(Lesson another) {
		Date start = this.getDate() ;
		Date end   = another.getDate() ;
		//return this.time - another.time ;
		return start.compareTo(end) ;
	}// compareTo(LEsson)

	protected void buildDate () {
		// Time representation example : 20/01/2012 10h00 - 12h00
		StringBuilder s = new StringBuilder(this.time) ;
		// Extract the beginning : 20/01/2012 10h00
		int pos = s.indexOf("-") ;
		while ((pos > 0) && ((s.charAt(pos) < '0') || ('9' < s.charAt(pos)))) {
			pos-- ;
		}// while
		s = new StringBuilder(s.substring(0, pos+1)) ;
		s.setCharAt(s.indexOf("h"), ':') ;
		try {
			this.date = new SimpleDateFormat("yyyy/MM/dd HH:mm").parse(s.toString()) ;
		} catch (ParseException e) {/* */}// try
	}

	public Date getDate () {
		if (this.date == null) {
			this.buildDate() ;
		}// if
		return this.date ;
	}// getDate()

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeLong(lessonId);
		parcel.writeString(title);
		parcel.writeString(time);
		parcel.writeString(schedule);
		parcel.writeInt(type);
		parcel.writeString(rooms);
		parcel.writeString(groups);
		parcel.writeString(teachers);
		parcel.writeString(created);
	}

	public static final Parcelable.Creator<Lesson> CREATOR = new Parcelable.Creator<Lesson>() {

		public Lesson createFromParcel(Parcel in) {
			return new Lesson(in);
		}

		public Lesson[] newArray(int pos) {
			return new Lesson[pos];
		}

	} ;
	
	@Override
	public boolean equals(Object o) {
		if (o instanceof Lesson) {
			Lesson other = (Lesson) o ;
			return this.time.equals(other.time)
					&& this.groups.equals(other.groups)
					&& this.rooms.equals(other.rooms)
					&& this.teachers.equals(other.teachers)
					&& this.title.equals(other.title) ;
		}// if
		return super.equals(o) ;
	}// equals(Object)

}// class Lesson
